﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Pacoca
{
    class Player
    {
        public static float velocidadePlayer = 4f, acelPulo = 10f, gravidade = 0.5f, velPulo;
        Vector2 posPlayer, posPlayerTemp;
        Texture2D texPlayer = Textura.texPlayer;
        public static int left, top, right, bottom, centerY; // tiles laterais do personagem
        Boolean puloLiberado, puloDescendo, puloSubindo, playerAgachado;



        float teste; 



        public Player()
        {
            Reiniciar();
        }

        public void Update(GameTime gameTime)
        {

            posPlayerTemp = posPlayer;
            Vector2 direcao = new Vector2(0, 0);
            KeyboardState keyboardState = Keyboard.GetState();

            //Testa o Input e diz a direcao lateral do Player
            direcao.X = InputMovimentacao(keyboardState);


            if ((keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.W)) && puloLiberado && !playerAgachado)
            {
                velPulo = -acelPulo;
                puloDescendo = false;
                puloSubindo = true;
                puloLiberado = false;
            }
            else
            {

                if (!puloLiberado)
                {
                    velPulo += gravidade;

                  
                        if (velPulo > 0)
                        {
                            puloDescendo = true;
                            puloSubindo = false;
                        }
                        else
                        {
                            puloSubindo = true;
                            puloDescendo = false;

                        }
                    
                }
            }




                #region Player Agachado
                
                TilesLaterais(new Vector2(0, -1f));
                if (keyboardState.IsKeyDown(Keys.Down) || keyboardState.IsKeyDown(Keys.S))
                {
                    texPlayer = Textura.texPlayerAgachado;
                }
                else
                {
                    if (texPlayer == Textura.texPlayerAgachado)
                    {
                        
                        if (TesteColisaoTile(left, top) || TesteColisaoTile(right, top))
                        {
                            texPlayer = Textura.texPlayerAgachado;
                        }
                        else
                        {
                            texPlayer = Textura.texPlayer;
                        }
                        
                    }

                }

                if (texPlayer == Textura.texPlayer)
                {
                    if (playerAgachado)
                    {
                        playerAgachado = false;
                        posPlayerTemp.Y -= 50;
                    }
                }
                else
                {
                    if (!playerAgachado)
                    {
                        playerAgachado = true;
                        posPlayerTemp.Y += 50;
                    }
                }

                #endregion

                #region ReacaoMovimentacaoLateral
                TilesLaterais(direcao);
                if (direcao.X == 1)
                {
                    if (TesteColisaoTile(right, top) || TesteColisaoTile(right, bottom) || TesteColisaoTile(right, centerY))
                    {
                        direcao.X = 0;
                        posPlayerTemp.X = ((right * 50) - (TileMap.mapPosition)) - 50;
                    }
                }
                if (direcao.X == -1)
                {
                    if (TesteColisaoTile(left, top) || TesteColisaoTile(left, bottom) || TesteColisaoTile(left, centerY))
                    {
                        direcao.X = 0;
                        posPlayerTemp.X = ((left * 50) - (TileMap.mapPosition)) + 50;
                    }
                }

                posPlayerTemp.X += (float)(direcao.X * velocidadePlayer);
                posPlayer.X = (int)posPlayerTemp.X;

                #endregion



                TilesLaterais(new Vector2(0, velPulo));
                if (puloLiberado)
                {
                    if (!TesteColisaoTile(left, bottom) || !TesteColisaoTile(right, bottom))
                    {
                        puloDescendo = true;
                        puloLiberado = false;
                        puloSubindo = false;
                        velPulo = 0.001f;
                    }
                }
                else
                {
                    if (!puloDescendo && puloSubindo)
                    {
                        if (TesteColisaoTile(left, top) || TesteColisaoTile(right, top))
                        {
                            velPulo = 0;
                            puloDescendo = true;
                            puloSubindo = false;
                            puloLiberado = false;
                        }

                    }
                    if (puloDescendo && !puloSubindo)
                    {
                        if (TesteColisaoTile(left, bottom) || TesteColisaoTile(right, bottom))
                        {
                            puloDescendo = false;
                            puloLiberado = true;
                            velPulo = 0;
                            if(playerAgachado)
                                posPlayerTemp.Y = (bottom - 1) * 50;
                            else
                                posPlayerTemp.Y = (bottom - 2) * 50;
                        }
                    }
                }
                posPlayerTemp.Y += velPulo;
                posPlayer.Y = (int)posPlayerTemp.Y;


                if (posPlayer.X + Textura.texPlayer.Width > Game1.tamanhoTela.X - Game1.tamanhoTela.X / 8)
                    posPlayer.X = Game1.tamanhoTela.X - Game1.tamanhoTela.X / 8 - Textura.texPlayer.Width;
                else if (posPlayer.X < 0)
                    posPlayer.X = 0;

                teste = bottom;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texPlayer, posPlayerTemp,Color.White);
            //spriteBatch.DrawString(Textura.spriteFonte, "posPlayer" + posPlayer.X + "   teste: " + teste +"\n posMap: "+ TileMap.mapPosition + "    Blocoleft: " + left*50, new Vector2(400, 300), Color.Red);  
        }



        public void TilesLaterais(Vector2 direcao)
        {
            // Define a nova posição do personagem
            Vector2 newPosition = new Vector2(posPlayerTemp.X + TileMap.mapPosition, posPlayerTemp.Y);
            newPosition.X += (direcao.X * velocidadePlayer);
            newPosition.Y += direcao.Y;
            // Define os tiles vizinhos do personagem conforme a nova posição
            left = (int)(newPosition.X / 50);
            top = (int)(newPosition.Y / 50);
            right = (int)((newPosition.X + (texPlayer.Width - 1)) / 50);
            bottom = (int)((newPosition.Y + (texPlayer.Height - 1)) / 50);


            if (!playerAgachado)
                centerY = bottom - 1;
            else
                centerY = bottom;
        }

        public bool TesteColisaoTile(int x, int y)
        {
            if (TileMap.tiles[TileMap.map[y, x]].collision != TileCollision.Impassable)
            {
                return false;
            }
            return true;

        }

        public int InputMovimentacao(KeyboardState Ks)
        {

            if (Ks.IsKeyDown(Keys.A) || Ks.IsKeyDown(Keys.Left))
            {
                return -1;
            }
            else if (Ks.IsKeyDown(Keys.D) || Ks.IsKeyDown(Keys.Right))
            {
                return 1;
            }
            else
            {
                if (!Background.paralaxParado)
                    return -1;
                else if (Ks.IsKeyDown(Keys.A) || Ks.IsKeyDown(Keys.Left))
                {
                    return -1;
                }
                else
                    return 0;
            }
        }

        public void Reiniciar()
        {
            posPlayer.Y = Game1.tamanhoTela.Y - Textura.texPlayer.Height - 50;
            posPlayer.X = 300;
            texPlayer = Textura.texPlayer;
        }

    }
}
